<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let user = [
            { name: '张三', js: 80 },
            { name: '李思', js: 90 },
            { name: '王武', js: 30 }
        ];
        // every 全部满足条件才返回真
        const res = user.every(item => {
            return item.js > 60
        })

        console.log(res);

        // some 有一个满足条件就为真
        const result = user.some(item => {
            return item.js > 60;
        })
        console.log(result);

        // filter数组过滤实现原理
        function filter(arr, cb) {
            let newArray = [];
            for (const value of arr) {
                if (cb(value) === true) {
                    newArray.push(value);
                }
            }
            return newArray;
        }
        // map 数组映射


        // reduce
        // prev 上一次返回的结果 value 当前值 index索引 arr原数组
        // arr.reduce((prev, value, index, array) => {

        // })
        // 统计某个数在数组
        function arrCount(array, value) {
            return array.reduce((total, curValue) => {
                total += value == curValue ? 1 : 0;
                return total;
            }, 0)
        }
        let arr = [1, 2, 3, 4, 5, 2, 3, 1, 1, 5];
        console.log(arrCount(arr, 1)); // 

        // reduce 查找最大值
        let arr1 = [1, 5, 3, 69, 7];
        function maxValue(array) {
            return array.reduce((prev, cur) => {
                return prev > cur ? prev : cur;
            })
        }
        console.log(maxValue(arr1));
        // reduce 求总和
        function sum(array) {
            return array.reduce((total, cur) => {
                return total += cur;
            }, 0);  // 初始为0
        }
        console.log(sum(arr1))

        let cart = [
            { name: '手机', price: 12000 },
            { name: 'ipad', price: 3600 },
            { name: '电脑', price: 36000 }
        ]
        // 获取超过10000的商品
        function getNameByPrice(goods, price) {
            return goods.reduce((arr, cur) => {
                if (cur.price > price) {
                    arr.push(cur);
                }
                return arr;
            }, []).map(item => item.name) // 初始是空数组

        }
        console.log(getNameByPrice(cart, 10000));

        // reduce 数组去重
        let arr0 = [1, 2, 3, 4, 5, 2, 3, 1, 1, 5, 6];
        let newArr = arr0.reduce(function(arr,cur){
            if(arr.includes(cur)===false){
                arr.push(cur);
            }
            return arr;
        },[])
        console.table(newArr)
    </script>
</body>

</html>